<?php

namespace App\Models\Entity\tag_content;

use Eloquent as Model;
use Illuminate\Database\Eloquent\SoftDeletes;
use App\Models\Entity\Asset\Video\Vasseti;
use App\Models\Entity\Asset\Vlive\Vchan;
use App\Models\Entity\Asset\Iaasset;

/**
 * Class tag_content
 * @package App\Models\Entity\tag_content
 * @version June 24, 2018, 1:20 pm CST
 *
 * @property integer tag_id
 * @property string content_model
 * @property integer content_id
 */
class tag_content extends Model
{
    use SoftDeletes;


    public $table = 'tag_contents';
    

    protected $dates = ['deleted_at'];

    public static $tag_content_type = array(
        'vasseti'=>'App\Models\Entity\Asset\Video\Vasseti',
        'vchan'=>'App\Models\Entity\Asset\Vlive\Vchan',
        'iaasset'=>'App\Models\Entity\Asset\Iaasset',
        'pic_album'=>'App\Models\Entity\pic_albums\pic_albums',
    );

    public $fillable = [
        'tag_id',
        'tag_content_type',
        'tag_content_id'
    ];

    /**
     * The attributes that should be casted to native types.
     *
     * @var array
     */
    protected $casts = [
        'tag_id' => 'integer',
        'tag_content_type' => 'string',
        'tag_content_id' => 'integer'
    ];

    /**
     * Validation rules
     *
     * @var array
     */
    public static $rules = [
        'tag_id' => 'required|integer',
        'tag_content_type' => 'required|max:100',
        'tag_content_id' => 'required|integer'
    ];


    public function tagContentList($where,$start,$length,$ord){
        $recordsTotal = $this->where($where)->count();
        $recordsFiltered = $recordsTotal;
        $data = $this->where($where)
                     ->select('id','tag_id','tag_content_type','tag_content_id')
                     ->orderByRaw($ord)
                     ->skip($start)->take($length)
                     ->get();
        // $modelVasseti = new Vasseti();
        // $modelVchan = new Vchan();
        foreach ($data as &$val){
            switch ($val['tag_content_type']) {
                case 'App\Models\Entity\Asset\Video\Vasseti':
                {
                    $val['tag_content_type']='点播';
                    $val['content'] = Vasseti::with(['vcat','vlangs','sps','tags'])->find($val['tag_content_id']);
                    $val['content']['content_no'] = $val['content']['vasseti_no'];
                    break;
                }
                case 'App\Models\Entity\Asset\Vlive\Vchan':
                {
                    $val['tag_content_type']='直播';
                    $val['content'] = Vchan::with(['vccat','vlangs','sps','tags'])->find($val['tag_content_id']);
                    $val['content']['content_no'] = $val['content']['vchan_no'];
                    break;
                }
                case 'App\Models\Entity\Asset\Iaasset':
                {
                    $val['tag_content_type']='音频';
                    $val['content'] = Iaasset::with(['vccat','vlangs','sps','tags'])->find($val['tag_content_id']);
                    $val['content']['content_no'] = $val['content']['vchan_no'];
                    break;
                }
                default:
                    break;
            }
        }
        return array($recordsTotal,$recordsFiltered,$data);
    }

}
